package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import command.CommandGenerator;

import card.Card;
import effect.Effect;
import effect.EffectUtil;
import effect.Effect.EffectType;

public class SelectedBar implements Observer{
        private static SelectedBar me;
        List<SelectionSphere> selectionSpheres = new ArrayList<SelectionSphere>();
        private Card selectedCard = null;
        
        public static SelectedBar getInstance(){
                if(me == null)
                        me = new SelectedBar();
                return me;
        }
        private SelectedBar(){
                GUICore.getInstance().addObserver(this);
        }
        
        
        public List<SelectionSphere> getSelectionSpheres(){
                return selectionSpheres;
        }
        
        
        public void paint(Graphics g){
                if(selectedCard != null){
                        ImageHandler.getInstance().drawImage(1, 1, 393, 136, "selectedCardImageFinal.png", g);

                        ImageHandler.getInstance().drawImage(25, 31, 83, 83, selectedCard.getImageSmall(), g);
                        Color playerColor = selectedCard.getOwner().getColor();
                        if(playerColor == Color.red)
                                ImageHandler.getInstance().drawImage(109, 39, 164, 26, "nameLabelRed.png", g);
                        else if(playerColor == Color.blue)
                                ImageHandler.getInstance().drawImage(109, 39, 164, 26, "nameLabelBlue.png", g);
                        else if(playerColor == Color.yellow)
                                ImageHandler.getInstance().drawImage(109, 39, 164, 26, "nameLabelYellow.png", g);
                        else if(playerColor == Color.green)
                                ImageHandler.getInstance().drawImage(109, 39, 164, 26, "nameLabelGreen.png", g);
                        
                        if(playerColor == Color.yellow)   //Selects color for text
                                g.setColor(Color.black);
                        else
                                g.setColor(Color.white);                        
                        g.setFont(new Font("Dialog", Font.BOLD, 12));
                        g.drawString(selectedCard.getName(), 126, 57);
                        
                        int effectPosX = 109;
                        for(Effect e : selectedCard.getEffects()){  // Draws all effects
                                ImageHandler.getInstance().drawImage(effectPosX, 12, 24, 24, e.getImageName(), g);
                                effectPosX += 26;
                                
                        }
                        
                        for(SelectionSphere s : selectionSpheres){
                                s.paint(g);
                        }
                }
        }
        @Override
        public void update(Observable arg0, Object arg1) {
                selectionSpheres.clear();
                
                SelectionSphere tempSphere = null;
                int sphereCount = 0;
                selectedCard=GUICore.getInstance().getSelectedCard();
                if(selectedCard != null){
                        if(CommandGenerator.getInstance().generateClaimTileCommand(selectedCard) != null){
                                tempSphere=new ClaimTileSphere(selectedCard);
                                tempSphere.setPosition(new Point(142+50*sphereCount, 92));
                                tempSphere.setSize(50);
                                sphereCount++;
                                selectionSpheres.add(tempSphere);
                                
                        }
                        for(Effect e : EffectUtil.getSubList(selectedCard.getEffects(), EffectType.activatable)){
                                tempSphere = new EffectSphere(selectedCard, e);
                                tempSphere.setPosition(new Point(142+50*sphereCount, 92));
                                tempSphere.setSize(50);
                                sphereCount++;
                                selectionSpheres.add(tempSphere);
                        }

                        
                }
                
        }
}